#import "AppDelegate.h"

#pragma mark - 首页控制器
@implementation home

/*===================================================================
 说明:
	目标控制器，必须在viewWillCreate中设置，如果在viewDidLoad中设置会造成
	子控制器无法接收viewWillApper等方法
 ==================================================================*/
- (void)viewWillCreate
{
    [super viewWillCreate];
    
    [self createOrderGoodsTable];
    
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    [self.tabBar setBackgroundImage:IMG(@"tab_bg")];
    
    UIViewController *v1 = [UIViewController storyboardInitialWithName:@"Home"];
    UIViewController *v2 = [UIViewController storyboardInitialWithName:@"PingLei"];
    UIViewController *v3 = [UIViewController storyboardInitialWithName:@"Car"];
    UIViewController *v4 = [UIViewController storyboardInitialWithName:@"User"];
    //改变导航栏颜色
    self.viewControllers = @[v1,v2,v3,v4];
    self.selectedIndex = 0;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLogin) name:USER_LOGIN object:nil];
}

- (void)userLogin
{
    self.selectedIndex = 0;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (![WJUser user].login)
    {
        [self presentViewController:[UIViewController storyboardWithName:@"Main" storyboardID:@"user"] animated:NO completion:nil];
    }
}

/*===================================================================
 说明:
	控制器
 ==================================================================*/
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
{
    [super setViewControllers:viewControllers animated:animated];
    
    //国际化内容
    for (UITabBarItem *item in self.tabBar.items)
    {
        [item setTitle:NSLocalized(item.title)];
    }
}

- (void)createOrderGoodsTable
{
    [dbase createTable:DB_GOODS
                fields:@[
                         @{@"●Gid"               :DB_INTEGER},      //商品数量
                         @{@"Count"             :DB_TEXT},      //商品数量
                         @{@"Gds_id"            :DB_TEXT},      //商品ID
                         @{@"Name"              :DB_TEXT},      //商品名称
                         @{@"Price"             :DB_TEXT},      //商品单价
                         @{@"TotalPrice"        :DB_TEXT},      //商品总价
                         @{@"Type"              :DB_TEXT},      //商品类型
                         @{@"Url"               :DB_TEXT},      //商品首图链接
                         @{@"Datetime"          :DB_TEXT},      //添加时间
                         ]];
}


@end

